<template>
    <view class="content" v-if="isShow">
        <view class="nav">
            <view class="nav-i" :class="{active : type == 6}" @click="chooseNav(6)">待卸车({{count.count6}})</view>
            <view class="nav-i" :class="{active : type == 7}" @click="chooseNav(7)">已完成({{count.count7}})</view>
        </view>
        
        <view class="u-m-20">
            <u-search placeholder="输入发货方联系方式或姓名" v-model="keyword" :show-action="false" bg-color="#fff" @search="doSearch"></u-search>
        </view>
        
        <view class="box" v-for="(item, index) in list" :key="index">
            <view class="top">
                <view class="item u-flex-1">
                    <view class="left">运单编号</view>
                    <view class="right">{{item.out_trade_no}}</view>
                </view>
                <view class="o-sta1" v-if="item.order_type == 1">零担</view>
                <view class="o-sta2" v-if="item.order_type == 2">包车</view>
            </view>
            <view class="item">
                <view class="left">运输线路</view>
                <view class="right">{{item.line}}</view>
            </view>
            <view class="item">
                <view class="left">司机</view>
                <view class="right">{{item.name}}</view>
            </view>
            <view class="item">
                <view class="left">车牌号</view>
                <view class="right">{{item.number}}</view>
            </view>
            <view class="item">
                <view class="left">联系方式</view>
                <view class="right">{{item.phone}}</view>
            </view>
            <view class="item">
                <view class="left">始发地</view>
                <view class="right">{{item.origin}}</view>
            </view>
            <view class="item">
                <view class="left">目的地</view>
                <view class="right">{{item.destination}}</view>
            </view>
            <view class="item">
                <view class="left">装车地点</view>
                <view class="right">{{item.loading_site}}</view>
            </view>
            <view class="item" v-if="item.seal_img">
                <view class="left">发货签封照片</view>
                <view class="right">
                    <image :src="item.seal_img" class="img" @click.stop="$previewImageSA(item.seal_img)"></image>
                </view>
            </view>
            
            <view class="item" v-if="type == 7 && item.kilometer_img">
                <view class="left">货物摆放照片</view>
                <view class="right">
                    <image :src="item.kilometer_img" class="img" @click.stop="$previewImageSA(item.kilometer_img)"></image>
                </view>
            </view>
            <view class="item" v-if="type == 7 && item.unload_seal_img">
                <view class="left">卸车签封</view>
                <view class="right">
                    <image :src="item.unload_seal_img" class="img" @click.stop="$previewImageSA(item.unload_seal_img)"></image>
                </view>
            </view>
			
            <view class="item" v-if="item.all_num">
                <view class="left">实际总件数</view>
                <view class="right">{{item.all_num}}件</view>
            </view>
            <view class="item" v-if="item.all_weight">
                <view class="left">实际总重量</view>
                <view class="right">{{item.all_weight}}斤</view>
            </view>
            <view class="item" v-if="item.weigh">
                <view class="left">过磅总重量</view>
                <view class="right">{{item.weigh}}吨</view>
            </view>
            <view class="item" v-if="item.load_start_time">
                <view class="left">装车开始时间</view>
                <view class="right">{{item.load_start_time}}</view>
            </view>
            <view class="item" v-if="item.load_end_time">
                <view class="left">装车结束时间</view>
                <view class="right">{{item.load_end_time}}</view>
            </view>
            <view class="item" v-if="item.start_date">
                <view class="left">发车时间</view>
                <view class="right">{{item.start_date}}</view>
            </view>
            <view class="item" v-if="item.expect_time">
                <view class="left">预计到达时间</view>
                <view class="right">{{item.expect_time}}</view>
            </view>
            <view class="item" v-if="item.arrival_time">
                <view class="left">司机到达时间</view>
                <view class="right">{{item.arrival_time}}</view>
            </view>
            <view class="item" v-if="type == 7 && item.unload_time">
                <view class="left">卸车开始时间</view>
                <view class="right">{{item.unload_time}}</view>
            </view>
            <view class="item" v-if="type == 7 && item.finish_time">
                <view class="left">卸车结束时间</view>
                <view class="right">{{item.finish_time}}</view>
            </view>
            
            <view class="btns">
				<view class="btn1" v-if="item.unload_seal_id > 0" @click.stop="toDetails(item)">查看详情</view>
                <view class="btn1" v-if="type == 6 && item.unload_seal_id == 0" @click.stop="unload(item)">开始卸车</view>
				<view class="btn1" v-if="type == 7" @click.stop="create(item)">下单</view>
            </view>
        </view>
		
		<view style="height: 94rpx;padding-top: 30rpx;">
		    <u-loadmore :status="status" v-if="status != 'loadmore'" />
		</view>
        
		<u-mask :show="show_1">
		    <view class="privacy-pop">
		        <view class="privacy-pop-box">
		            <view class="close-01">
		                <u-icon name="close-circle" size="50" @click="show_1 = false"></u-icon>
		            </view>
		            <view class="privacy-title">上传签封照片</view>
		            <view>
		                <image :src="unload_seal || '../../static/image/up.png'" class="up" @click.stop="chooseImage"></image>
		            </view>
		            <view class="btn-text2" @click.stop="confirm">提交</view>
		        </view>
		    </view>
		</u-mask>
    </view>
</template>

<script>
    export default {
        data() {
            return {
				isShow: false,
				show_1: false,
                type: 6,
				keyword: '',
				status: 'loadmore', //是否显示没有更多了
				page: 1,
				list: [],
				count: {},
				item: {},
				unload_seal_id: '',//签封照片
				unload_seal: '',
            };
        },
		onLoad(option) {
			this.type = option.type
			this.getList()
			// uni.$on('UNLOAD_BACK', data => {
			// 	this.list = []
			// 	this.page = 1
			// 	this.status = 'loadmore'
			// 	this.getList()
			// })
			// uni.startPullDownRefresh();
		},
		onShow() {
			// if (!this.$getSync('UN_IMAGE')) {
			// 	this.list = []
			// 	this.page = 1
			// 	this.status = 'loadmore'
			// 	this.getList()
			// }
		},
		onPullDownRefresh() {
			let _this = this
			console.log('refresh');
			setTimeout(() => {
				_this.list = [];
				_this.page = 1;
				_this.status = 'loadmore';
				_this.getList();
				uni.stopPullDownRefresh();
			}, 1000);
		},
        onReachBottom() {
        	if(this.page == 1){return}
        	this.getList()
        },
        methods: {
			// 开始卸车
			unload(item) {
				this.item = item
				this.show_1 = true
			},
			confirm() {
				if (!this.unload_seal_id) {
					this.$toast('请上传签封照片')
					return
				}
				this.$ajax('unload_seal', {
					user_token: this.$getSync('userToken'),
					order_id: this.item.id,//运单id
					unload_seal_id: this.unload_seal_id,//卸货签封图片id
				}).then(ret => {
					if (ret.success == 1000) {
						this.show_1 = false
						this.unload_seal_id = ''
						this.unload_seal = ''
						this.$gTo(`/pages/order/complete?id=${this.item.id}`)
					} else {
						this.$toast(ret.msg);
					}
				});
			},
			
			chooseImage() {
				this.$setSync('UN_IMAGE', 1)
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album', 'camera'], // 相机拍照 album 相册、camera 拍照
				    success: (res) => {
				        this.$upLoadPic(res.tempFilePaths[0]).then(ret => {
				            console.log(ret)
							let data = ret.detail
							this.unload_seal_id = data.image_id//签封照片
							this.unload_seal = data.image
							this.$removeSync('UN_IMAGE')
				        });
				    }
				});
			},
			
			// 下单
			create(item) {
				this.$gTo(`/pages/order/create?driver_id=${item.id}&order_type=${item.order_type}`)
			},
			
            toDetails(item) {
				this.$gTo(`/pages/order/complete?id=${item.id}`)
                // if (this.type == 1) {
                //     // 零担
                //     // this.$gTo(`/pages/order/details`)
                //     // 包车
                //     this.$gTo(`/pages/order/details-2`)
                // } else {
                //     this.$gTo(`/pages/order/complete`)
                // }
            },
			
            chooseNav(num) {
				if (this.type == num) return
                this.type = num
				this.doSearch()
            },
			
			doSearch() {
				this.list = []
				this.page = 1
				this.status = 'loadmore'
				this.getList()
			},
			
			getList() {
				if (this.status == 'nomore') return;
				this.status = 'loading';
				this.$ajax('unload_waybill', {
					user_token: this.$getSync('userToken'),
					page: this.page,
					limit: 10,
			        status: this.type,//6待卸货 7已完成 8异常订单
					name: this.keyword,
				}).then(ret => {
					if (ret.success == 1000) {
						this.count = ret.detail
						
						if (ret.detail && ret.detail.orders && ret.detail.orders.length > 0) {
							this.list = this.list.concat(ret.detail.orders);
							this.page++;
							this.status = 'loadmore'
						} else {
							this.status = 'nomore'
						}
						this.isShow = true
					} else {
						this.$toast(ret.msg);
					}
				});
			},
        }
    };
</script>

<style lang="scss">
	.privacy-pop {
	    display: flex;
	    align-items: center;
	    justify-content: center;
	    height: 100%;
	}
	.privacy-pop-box {
	    width: 700rpx;
	    padding: 40rpx 20rpx 20rpx;
	    background-color: #fff;
	    box-sizing: border-box;
	    border-radius: 20rpx;
	    position: relative;
	}
	.close-01{
	    position: absolute;
	    right: 20rpx;
	    top: 20rpx;
	}
	.privacy-title {
	    font-size: 32rpx;
	    font-weight: bold;
	    text-align: center;
	    padding: 0 0 40rpx;
	}
	.t-inp{
	    width: 100%;
	    background-color: #f5f6fa;
	    border-radius: 10rpx;
	    padding: 20rpx;
	}
	.btn-text2{
	    font-size: 30rpx;
	    color: #fff;
	    background-color: #00457f;
	    padding: 20rpx 50rpx;
	    border-radius: 20rpx;
	    margin: 20rpx;
	    text-align: center;
	}
	
	.up{
	    width: 187rpx;
	    height: 187rpx;
	    margin-top: 20rpx;
		margin: auto;
	}
	.d-btn{
	    font-size: 34rpx;
	    color: #fff;
	    text-align: center;
	    background-color: #00457f;
	    padding: 25rpx;
	    border-radius: 50rpx;
	    margin: 40rpx 0;
	}
	
	
    .img{
        width: 240rpx;
        height: 170rpx;
    }
    
    
    page{
        background-color: #eef1f6;
    }
    .content{
        padding-bottom: 50rpx;
    }
    .nav{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 30rpx 0;
        border-bottom: 2rpx solid #ebebeb;
        background-color: #fff;
    }
    .nav-i{
        width: calc(100vw / 2);
        font-size: 28rpx;
        text-align: center;
        position: relative;
    }
    .ord-num{
        font-size: 20rpx;
        line-height: 1;
        color: #fff;
        padding: 5rpx 8rpx;
        background-color: #fa3c07;
        border-radius: 50%;
        position: absolute;
        right: -5rpx;
        top: -20rpx;
    }
    .ord-num2{
        font-size: 20rpx;
        line-height: 1;
        color: #fff;
        padding: 6rpx 4.5rpx;
        background-color: #fa3c07;
        border-radius: 50%;
        position: absolute;
        right: -5rpx;
        top: -20rpx;
    }
    .active{
        color: #004580;
        font-weight: bold;
        position: relative;
    }
    .active::after{
        display: block;
        content: '';
        width: 32rpx;
        height: 8rpx;
        background-color: #004580;
        position: absolute;
        bottom: -30rpx;
        left: 50%;
        transform: translateX(-50%);
    }
    
    .box{
        background-color: #fff;
        border-radius: 15rpx;
        padding: 40rpx 30rpx 30rpx;
        margin: 16rpx 20rpx 0;
    }
    .top{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .item{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 10rpx;
    }
    .left{
        width: 185rpx;
        color: #656565;
    }
    .right{
        flex: 1;
        word-break: break-all;
        white-space: pre-line;
    }
    .o-sta{
        font-size: 24rpx;
        color: #ff602c;
        padding-bottom: 10rpx;
        padding-left: 10rpx;
    }
    .o-sta1{
        font-size: 24rpx;
        color: #ff602c;
        padding: 7rpx 15rpx;
        background-color: #fff1ec;
        border-radius: 10rpx;
        margin-left: 10rpx;
        margin-bottom: 10rpx;
    }
    .o-sta2{
        font-size: 24rpx;
        color: #0075ff;
        padding: 7rpx 15rpx;
        background-color: #ebf4fe;
        border-radius: 10rpx;
        margin-left: 10rpx;
        margin-bottom: 10rpx;
    }
    .price{
        font-size: 26rpx;
        color: #ff602c;
    }
    .price-num{
        font-size: 38rpx;
    }
    .btns{
        padding-top: 10rpx;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        color: #fff;
    }
    .btn1{
        padding: 15rpx 25rpx;
        background-color: #004580;
        border-radius: 50rpx;
        margin-left: 20rpx;
    }
    .btn2{
        padding: 15rpx 25rpx;
        background-color: #9aafc2;
        border-radius: 50rpx;
        margin-left: 20rpx;
    }
    .btn3{
        padding: 15rpx 25rpx;
        background-color: #fff;
        border-radius: 50rpx;
        margin-left: 20rpx;
        color: #004580;
        border: 2rpx solid #004580;
        box-sizing: border-box;
    }
    
    
    .popup{
        padding: 30rpx;
    }
    .pop-title{
        font-size: 34rpx;
        font-weight: bold;
        text-align: center;
        padding-bottom: 40rpx;
    }
    .pop-tip{
        font-size: 24rpx;
        color: #fe6a00;
        padding-top: 20rpx;
        padding-bottom: 50rpx;
    }
    .t-inp{
        padding: 20rpx;
        background-color: #f5f5f5;
        border-radius: 10rpx;
        font-size: 26rpx;
        width: 100%;
        min-height: 220rpx;
    }
    .pop-btn{
        font-size: 30rpx;
        color: #fff;
        text-align: center;
        padding: 20rpx;
        background-color: #00457f;
        border-radius: 50rpx;
    }
    

</style>
